package com.wbao.dianniu.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.wbao.dianniu.R;
import com.wbao.dianniu.data.RecruitListData;

/**
 * 岗位管理
 *
 * @author yejianjun
 */
public class RecruitManagerAdapter extends BaseAdapter {
    private LayoutInflater mInflater;
    private List<RecruitListData> mList;
    private Context mContext;

    public RecruitManagerAdapter(Context context) {
        mContext = context;
        mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    public void setData(List<RecruitListData> list) {
        mList = list;
    }

    public void addData(List<RecruitListData> list) {
        if (null == mList) {
            mList = new ArrayList<RecruitListData>();
        }
        for (int i = 0; i < list.size(); i++) {
            mList.add(list.get(i));
        }
    }

    public void clear() {
        mList.clear();
        notifyDataSetChanged();
    }

    public List<RecruitListData> getList() {
        return mList;
    }

    public void addTopData(RecruitListData data) {
        mList.add(0, data);
    }

    public void updateStatus(int id, int status) {
        for (int i = 0; i < mList.size(); i++) {
            if (mList.get(i).getId() == id) {
                mList.get(i).setStatus(status);
                if (1 == status) {
                    mList.get(i).setStatusDesc("正在招聘");
                } else {
                    mList.get(i).setStatusDesc("已停止招聘");
                }
                notifyDataSetChanged();
                break;
            }
        }
    }

    @Override
    public int getCount() {
        if (null == mList)
            return 0;
        else
            return mList.size();
    }

    @Override
    public Object getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        GridHolder holder;
        if (convertView == null) {
            convertView = mInflater.inflate(R.layout.recruit_manager_item, null);
            holder = new GridHolder();
            holder.postTV = convertView.findViewById(R.id.recruit_manager_subject);
            holder.salaryTV = convertView.findViewById(R.id.recruit_manager_salary);
            holder.areaTV = convertView.findViewById(R.id.recruit_manager_area);
            holder.educationTV = convertView.findViewById(R.id.recruit_manager_item_education);
            holder.experienceTV = convertView.findViewById(R.id.recruit_manager_item_experience);
            holder.isTryoutTV = convertView.findViewById(R.id.recruit_manager_item_tryout);
            holder.browseTV = convertView.findViewById(R.id.recruit_manager_browseCount);
            holder.deliveryTV = convertView.findViewById(R.id.recruit_manager_deliveryCount);
            holder.statusTV = convertView.findViewById(R.id.recruit_manager_status);
            convertView.setTag(holder);
        } else {
            holder = (GridHolder) convertView.getTag();
        }
        RecruitListData data = mList.get(position);
        if (null != data) {
            holder.postTV.setText(data.getPost() == null ? "" : data.getPost());
            holder.salaryTV.setText(data.getSalary() == null ? "" : "" + data.getSalary());
            holder.areaTV.setText(data.getAddress() == null ? "" : "" + data.getAddress());
            holder.educationTV.setText(data.getEducation() == null ? "" : data.getEducation());
            holder.experienceTV.setText(data.getExperience() == null ? "" : data.getExperience());
            holder.isTryoutTV.setText(data.getTryout() == null ? "" : data.getTryout());
            holder.browseTV.setText("浏览：" + data.getBrowseCount() + "次");
            holder.deliveryTV.setText("应聘：" + data.getDeliveryCount() + "次");
            holder.statusTV.setText(data.getStatusDesc() == null ? "" : data.getStatusDesc());
            if (1 == data.getStatus()) {
                holder.statusTV.setTextColor(mContext.getResources().getColor(R.color.head_name_color));
            } else {
                holder.statusTV.setTextColor(mContext.getResources().getColor(R.color.edit_hint_color));
            }
        }
        return convertView;
    }

    public class GridHolder {
        public TextView postTV;
        public TextView salaryTV;
        public TextView areaTV;
        public TextView educationTV;
        public TextView experienceTV;
        public TextView isTryoutTV;
        public TextView browseTV;
        public TextView deliveryTV;
        public TextView statusTV;
    }

}
